﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using BackgroundTest;
using BackgroundTest.BasicAbstracts;

namespace BackgroundTest.ConsoleComponents
{
    public class ConsoleComponent : DrawableGameComponent 
    {
        private SpriteBatch spriteBatch;
        private SpriteFont consoleFont;
        private float interline;
        private Color textColor;
        private bool Transparent;
        private int numberOfMessages;
        private List<string> messages;

        private Texture2D backgroundTexture;
        private Rectangle screen;
        private bool isChinese;

        private SpriteFont[] consoleChineseFont;
        private int extraChineseLine;

        public ConsoleComponent(BackGroundTest game, float interline, Color defaultTextColor, bool Transparent, bool isChinese)
            : base(game) 
        { 
            this.interline = interline;
            this.textColor = defaultTextColor;
            this.Transparent = Transparent;
            this.messages = new List<string>();
            this.isChinese = isChinese;
            extraChineseLine = 0;

            spriteBatch = new SpriteBatch(game.GraphicsDevice);
            if (isChinese)
            {
                consoleChineseFont = new SpriteFont[15];
                for (int i = 0; i <= 14; i++ )
                {
                    consoleChineseFont[i] = game.Content.Load<SpriteFont>("Fonts\\SongTi\\Song"+i);
                }
            }
            else
            {
                consoleFont = game.Content.Load<SpriteFont>(@"Fonts\Cambria");
            }

            backgroundTexture = game.Content.Load<Texture2D>(@"Background\black");

            screen = new Rectangle(0, 0, BasicData.WindowWidth, BasicData.WindowHeight);

            numberOfMessages = (int)(BasicData.WindowHeight / interline - 2);
        }

        public ConsoleComponent(BackGroundTest game, float interline, Color defaultTextColor, bool Transparent)
            : this(game,interline,defaultTextColor,Transparent,false)
        {

        }
        
        public void Clear() 
        {
            messages.Clear();
        } 
        
        public void WriteLine() 
        {
            messages.Add(""); 
        } 
        
        public void WriteLine(string text) 
        { 
            messages.Add(text);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (messages.Count>numberOfMessages)
            {
                messages.RemoveAt(0);
            }
        }
        
        public override void Draw(GameTime gameTime) 
        { 
            base.Draw(gameTime);
            spriteBatch.Begin();
            float y = interline;
            float x = interline;
            float deltaXChinese = 20;
            float deltaXEnglish = 10;

            if (Transparent)
            {
                spriteBatch.Draw(backgroundTexture,screen,new Color(255,255,255,200));
            }
            else
            {
                spriteBatch.Draw(backgroundTexture, screen, Color.White);
            }

            foreach (string text in messages)
            { 
                if (isChinese)
                {
                    foreach (char ch in text)
                    {
                        int codeCh = (int)ch;
                        if (codeCh>=10 && codeCh <=126 || codeCh>=65291 && codeCh<=65374 || codeCh>=12288 && codeCh<=12290)
                        {
                            spriteBatch.DrawString(consoleChineseFont[0],ch.ToString(),new Vector2(x,y),textColor);
                            x += deltaXEnglish;
                        }
                        else
                        {
                            int number = (int)Math.Floor((double)((codeCh-19068)/1500))+1;
                            spriteBatch.DrawString(consoleChineseFont[number], ch.ToString(), new Vector2(x, y), textColor);
                            x += deltaXChinese;
                        }
                        if (x>BasicData.WindowWidth-interline)
                        {
                            x = interline;
                            y += interline;
                            extraChineseLine += 1;
                        }
                    }
                    x = interline;
                }
                else
                    spriteBatch.DrawString(consoleFont, text, new Vector2(interline, y), textColor);
                y += interline; 
            }
            spriteBatch.End();
        } 
    }
}
